package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

import com.example.myapplication.R;
import com.example.myapplication.fragments.HomeFragment;
import com.example.myapplication.fragments.MatchFragment;
import com.example.myapplication.fragments.MineFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager2 viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);

        // 设置ViewPager2适配器
        viewPager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                // 根据位置返回对应的Fragment
                switch (position) {
                    case 0:
                        return new HomeFragment();
                    case 1:
                        return new MatchFragment();
                    case 2:
                        return new MineFragment();
                    default:
                        return new HomeFragment();
                }
            }

            @Override
            public int getItemCount() {
                return 3; // 三个页面
            }
        });

        // 将TabLayout与ViewPager2关联
        new TabLayoutMediator(tabLayout, viewPager,
                (tab, position) -> {
                    // 设置Tab的文本和图标
                    switch (position) {
                        case 0:
                            tab.setText("首页");
                            tab.setIcon(R.drawable.ic_home);
                            break;
                        case 1:
                            tab.setText("赛事");
                            tab.setIcon(R.drawable.ic_match);
                            break;
                        case 2:
                            tab.setText("我的");
                            tab.setIcon(R.drawable.ic_mine);
                            break;
                    }
                }
        ).attach();
    }
}